package com.bdqn.service.impl;

import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import com.bdqn.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * SysUserServiceImpl
 *
 * @author LILIBO
 * @since 2024/6/25
 */
public class SysUserServiceImpl implements SysUserService {

    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public int add(SysUser sysUser) {
        int row = 0;
        // 通过工具类获取SqlSession对象
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            // 使用Mapper接口执行数据库操作
            row = sqlSession.getMapper(SysUserMapper.class).add(sysUser);
        } catch (Exception e) {
            e.fillInStackTrace();
        } finally {
            // 释放资源
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return row;
    }

    /**
     * 修改用户信息
     *
     * @param sysUser
     * @return
     */
    @Override
    public int update(SysUser sysUser) {
        return 0;
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Long id) {
        return 0;
    }

    /**
     * 通过多参数查询
     *
     * @param rname
     * @param rid
     * @return
     */
    @Override
    public List<SysUser> getUsersByParams(String rname, Long rid) {
        return Collections.emptyList();
    }

    /**
     * 根据条件查询总数量
     *
     * @param rname
     * @param rid
     * @return
     */
    @Override
    public int countPage(String rname, Long rid) {
        int count = 0;
        // 通过工具类获取SqlSession对象
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            // 使用Mapper接口执行数据库操作
            count = sqlSession.getMapper(SysUserMapper.class).countPage(rname, rid);
        } catch (Exception e) {
            e.fillInStackTrace();
        } finally {
            // 释放资源
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 通过多参数分页查询
     *
     * @param rname
     * @param rid
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public List<SysUser> selectPage(String rname, Long rid, Long pageIndex, Long pageSize) {
        List<SysUser> list = new ArrayList<>();
        // 通过工具类获取SqlSession对象
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            // 使用Mapper接口执行数据库操作
            list = sqlSession.getMapper(SysUserMapper.class).selectPage(rname, rid, pageIndex, pageSize);
        } catch (Exception e) {
            e.fillInStackTrace();
        } finally {
            // 释放资源
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    /**
     * 账号密码登录
     *
     * @param account 账号
     * @param password 密码
     * @return 登录用户信息
     */
    @Override
    public SysUser login(String account, String password) {
        if (account == null || account.isEmpty() || password == null || password.isEmpty()) {
            return null;
        }
        SysUser sysUser = null;
        // 通过工具类获取SqlSession对象
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            // 使用Mapper接口执行数据库操作
            sysUser = sqlSession.getMapper(SysUserMapper.class).getByAccountAndPassword(account, password);
        } catch (Exception e) {
            e.fillInStackTrace();
        } finally {
            // 释放资源
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return sysUser;
    }

}
